<template>
  <el-dialog :title="dialogProps.title" :visible.sync="dialogProps.visible" width="560px"
    @open="handleDialogOpen()" v-loading="loading">
    <div slot="title" class="dialog-header">{{ dialogProps.title }}</div>
    <el-col :span="6">
          <el-select placeholder="请输入车辆类型" v-model="search.truckTypeId" style="width:100%">
            <el-option
            v-for="item in parentTruckType"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"></el-option>
          </el-select>
        </el-col>
        <el-col :span="6">
           <el-input placeholder="请输入车牌号码" size="small" v-model="search.licensePlate" style="width:100%">
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-select placeholder="请输入车队名称" v-model="search.fleetId" style="width:100%">
            <el-option
              v-for="item in parentFleet"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"></el-option>
          </el-select>
        </el-col>
        <el-col :span="6">
          <el-button type="danger"  size="small" @click="queryAll()">搜索</el-button>
        </el-col>

        <el-col :span="24">
          <el-table :data="tableData" border style="width: 100%">
            <el-table-column prop="id" label="车辆编号" header-align="center" width="120"></el-table-column>
            <el-table-column prop="truckTypeId" label="车辆类型" header-align="center" width="120"></el-table-column>
            <el-table-column prop="licensePlate" label="车牌号码" header-align="center" width="120"></el-table-column>
            <el-table-column prop="fleetId" label="所属车队" header-align="center" width="120"></el-table-column>
            <el-table-column prop="deviceGpsId" label="GPS设备ID" header-align="center" align="center" width="120"></el-table-column>
            <el-table-column prop="allowableLoad" label="准载重量(千克)" header-align="center" align="center" width="120"></el-table-column>
            <el-table-column prop="allowableVolume" label="准载体积(方)" header-align="center" align="center" width="120"></el-table-column>
            <!-- <el-table-column prop="transportTripsId" label="车次编号" header-align="center" align="center" width="120"></el-table-column> -->
            <el-table-column label="操作" header-align="center" align="center" width="200">
              <template slot-scope="scope">
                <el-button v-show="detail" type="primary" size="mini"
                  @click="handleDetailtruck(scope.row)">查看详情</el-button>
                <el-button  type="danger" size="mini"
                  @click="handleDeletetruck(scope.row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-row>
        <el-col :span="24">
         <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="currentChange"
            :current-page.sync="search.page"
            :page-sizes="[2,4,6,8]"
            :page-size="search.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="counts">
          </el-pagination>
        </el-col>
      </el-row>
        </el-col>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleDialogClose()">取 消</el-button>
      <el-button type="primary" size="small" @click="submitForm('FleetForm')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import teamApi from '@/api/team.js'
export default {
   name: "fleet-edit",
    data() {
    return {
      loading: false,
      fleetModel: {
        name:"",
        manager: ''
      },
      dialogProps: {
        visible: false,
        action: '',
        title: ''
      },
      formfleet: {
          name:[{
              required: true, message: '请输入车队名称', trigger: 'blur'
          }],
          manager:[{
              required:true,message:"请输入负责人",trigger:'blur'
          }]
      }
    }
 },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if(this.dialogProps.action === 'add') {
            this.doSaveFleet()
          } else {
            this.doUpdateFleet()
          }
        } else {
          return false
        }
      })
    },
    doUpdateFleet() {
      // this.loading = true
      // updateEmp(this.EmpModel)
      // .then(response => {
      //   this.loading = false
      //   this.dialogProps.visible = false
      //   this.$emit('save-finished')
      // })
      // .catch(error => {
      //   this.outputError(error)
      // })
    },
    doSaveFleet() {
      this.loading = true
      teamApi.saveFleet(this.fleetModel)
      .then(res => {
        console.log(res)
       this.loading = false
        this.dialogProps.visible = false
        this.$emit('save-finished')
      })
      .catch(err => {
        console.error(err);
      })
    },
    handleDialogClose() {
      this.dialogProps.visible = false
    },
    handleDialogOpen() {
      this.$nextTick(() => {
        this.$refs['FleetForm'].clearValidate()
      })
    },
    outputError(error) {
      console.log(error.response ? error.response : error.message)
      this.loading = false
      this.$message({
        showClose: true,
        message: '出错了，请按F12查看浏览器日志。',
        type: 'error'
      })
    }
  },
   mounted: function() {
    this.$nextTick(() => {
      this.$on('openEditFleetDialog', function() {
        this.dialogProps.action = 'edit'
        this.dialogProps.title = '编辑车队'
        this.dialogProps.visible = true
      })
      this.$on('openTruckDetailDialog', function() {
        this.dialogProps.action = 'truckDetail'
        this.dialogProps.title = '车辆详情'
        this.dialogProps.visible = true
      })
    })
  }
}
</script>

<style>

</style>
